﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.Text;
using Runda.CMS.Common.Utility;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.ContractManage
{
    public partial class ContractQuery : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindContractList(1);
            }
        }

        private void BindContractList(int pageindex)
        {
            StringBuilder sb = new StringBuilder();

            //sb.Append(" and CreatedBy = '").Append(base.CurrentUserID).Append("'");
            sb.Append(" and Status > ").Append((int)Enumerator.ContractStatus.Draft - 1);

            if (txtContractCodeBasic.Text.Trim() != string.Empty)
            {
                sb.Append(" and ContractCode = '").Append(txtContractCodeBasic.Text).Append("'");
            }

            if (txtCumtomerName.Text.Trim() != string.Empty)
            {
                sb.Append(" and CustomerName like '%").Append(txtCumtomerName.Text.Trim()).Append("%'");
            }

            if (txtInstrumentName.Text.Trim() != string.Empty)
            {
                sb.Append(" and ContractID in (select ContractID from Cms_Ctrt_ContractItem Item JOIN Cms_Ctrt_Instrument Inst ON item.InstrumentGUID = Inst.InstrumentGUID where Inst.InstrumentName like '%").Append(txtInstrumentName.Text.Trim()).Append("%')");
            }

            int RecordCount = 0;
            DataTable dt = new ContractBll().QueryContract(sb.ToString(), "createdon desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterContract.DataSource = dt;
            RepeaterContract.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindContractList(1);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindContractList(AspNetPager1.CurrentPageIndex);
        }

        public string GetStatusText(int status)
        {
            string text = string.Empty;
            EnumeratorHelper[] cs = EnumeratorHelper.GetFieldTexts(typeof(Enumerator.ContractStatus));
            foreach (EnumeratorHelper c in cs)
            {
                if (c.EnumValue == status)
                {
                    text = c.EnumDisplayText; break;
                }
            }

            return text;
        }

        protected void RepeaterContract_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindContractList(1);
            }
        }
 

        protected void RepeaterContract_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView dv = e.Item.DataItem as DataRowView;
                Label lblContractType = e.Item.FindControl("lblContractType") as Label;
                if (dv["ContractType"] != DBNull.Value)
                {
                    lblContractType.Text = EnumeratorHelper.GetFieldText((Enumerator.ContractType)Convert.ToInt32(dv["ContractType"].ToString()));
                }
            }
        }
    }
}
